package cn.edu.dgut.css.sai.lab2demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@EnableConfigurationProperties(CustomProperties.class)
public class Lab2AutoConfig {

    @Bean
    @ConditionalOnProperty(prefix = "sai.auto",name="enable",havingValue = "true")
    CommandLineRunner creatCustomCommandLineRunner(Environment env){
        return new CustomCommandLineRunner(env);
    }
    static class NoticeEvent extends ApplicationEvent {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(cn.edu.dgut.css.sai.lab2demo.NoticeEvent.class);
    private final String message;

    public NoticeEvent(String message) {
        super(message);
        this.message = message;
        logger.info("添加事件成功！message:{}", message);

    }
        public String getMessage(){return message;}


}
    @Component
    static class NoticeListener implements ApplicationListener<cn.edu.dgut.css.sai.lab2demo.NoticeEvent>{
        private static final Logger logger= LoggerFactory.getLogger(cn.edu.dgut.css.sai.lab2demo.NoticeListener.class);


        @Override
        public void onApplicationEvent(cn.edu.dgut.css.sai.lab2demo.NoticeEvent noticeEvent){
            logger.info("事件监听器获取到NoticeEvent，睡眠当前线程2秒...");
            try{

                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            logger.info("NoticeEvent的message属性是：{}",noticeEvent.getMessage());
        }
    }

}
